package com.ibsol.database
{
	import com.ibsol.model.Weather;
	
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	public class WeatherDAO{
		
		private var _tourixDBConn:SQLConnection;
		
		public function WeatherDAO(){
			_tourixDBConn=DBConnManager.getConnection("tourix");
		}
		
		/**
		 * 根据城市id得到天气信息
		 * @param cityid 城市编号
		 * @param resultHandler 回调函数 返回天气对象，如果没有，则返回null
		 * @param faultHandler 错误函数
		 **/
		 public function getWeatherByCityId(cityid:int,resultHandler:Function,faultHandler:Function=null):void{
		 	var sqlState:SQLStatement=new SQLStatement();
		 	sqlState.sqlConnection=_tourixDBConn;
		 	sqlState.text="select * from weather where cityid="+cityid;
		 	sqlState.addEventListener(SQLEvent.RESULT,function (e:SQLEvent):void{
		 		var arr:Array=sqlState.getResult().data;
		 		if(arr!=null&&arr.length>0)
		 			resultHandler.call(this,typeObject(arr[0]));
		 		else
		 			resultHandler.call(this,null);
		 	});
		 	sqlState.addEventListener(SQLErrorEvent.ERROR,faultHandler==null?sqlErrorHandler:faultHandler);
		 	sqlState.execute();
		 }
		 
		 /**
		 * 通过城市编号更新城市的天气情况 ，从yahoo weatherService中得到的数据
		 * @ param weather 天气对象
		 * @ param resultHandler 回调函数，如果更新成功。调用后，设置true
		 * @ param faultHandler 错误处理函数
		 **/
		 public function updateWeatherById(weather:Weather,resultHandler:Function=null,faultHandler:Function=null):void{
		 	var sqlState:SQLStatement=new SQLStatement();
		 	sqlState.sqlConnection=_tourixDBConn;
		 	sqlState.text="update weather set celcius='"+weather.celcius+"',forecast='"+weather.forecast+"',updateTime='"+weather.updateTime+"',offlineOperator='EDIT',high='"+
		 		weather.high+"',low='"+weather.low+"',humidity='"+weather.humidity+"',visibility='"+weather.visibility+"',des='"+weather.des+
		 		"',pictureid='"+weather.pictureid+"' where cityid="+weather.cityid;
		 	sqlState.addEventListener(SQLEvent.RESULT,function ():void{
		 		if(resultHandler!=null)
		 			resultHandler.call(this);
		 	});
		 	sqlState.addEventListener(SQLErrorEvent.ERROR,faultHandler==null?sqlErrorHandler:faultHandler);
		 	sqlState.execute();
		 }
		 
		 /**
		 * 按城市编号修改当前温度
		 * @ param cityid 城市编号
		 * @ param celcius 当前温度
		 * @ param resultHandler 回调函数
		 * @ param faultHandler 错误处理函数
		 **/
		 public function updateCelCiusById(cityid:int,celcius:String,resultHandler:Function,faultHandler:Function=null):void{
		 	var sqlState:SQLStatement=new SQLStatement();
		 	sqlState.sqlConnection=_tourixDBConn;
		 	sqlState.text="update weather set celcius='"+celcius+"' where cityid="+cityid;
		 	sqlState.addEventListener(SQLEvent.RESULT,function ():void{
		 		if(resultHandler!=null)
		 			resultHandler.call(this);
		 	});
		 	sqlState.addEventListener(SQLErrorEvent.ERROR,faultHandler==null?sqlErrorHandler:faultHandler);
		 	sqlState.execute();
		 }
		 
		 
		 /**
		 * 添加城市天气情况
		 * @ param weather 天气对象
		 * @ param resultHandler 回调函数
		 * @ param faultHandler 错误函数
		 **/
		 public function insertWeather(weather:Weather,resultHandler:Function=null,faultHandler:Function=null):void{
		 	var sqlState:SQLStatement=new SQLStatement();
		 	sqlState.sqlConnection=_tourixDBConn;
		 	sqlState.text="insert into weather(cityid,celcius,forecast,updateTime,offlineOperator,high,low,humidity,visibility,des,pictureid) values("+
		 		weather.cityid+",'"+weather.celcius+"','"+weather.forecast+"','"+weather.updateTime+"','"+weather.offlineOperator+"','"+weather.high+"','"+weather.low+"','"+
		 		weather.humidity+"','"+weather.visibility+"','"+weather.des+"','"+weather.pictureid+"')";
		 	sqlState.addEventListener(SQLEvent.RESULT,function(e:SQLEvent):void{
		 		weather.id=sqlState.getResult().lastInsertRowID;
		 		if(resultHandler!=null)
		 			resultHandler.call(this);
		 	});
		 	sqlState.addEventListener(SQLErrorEvent.ERROR,faultHandler==null?sqlErrorHandler:faultHandler);
		 	sqlState.execute();
		 }
		 
		 /**
		 * 封装数据库返回的object成Weather
		 * @param o 数据库返回对象
		 **/
		 private function typeObject(o:Object):Weather{
		 	var w:Weather=new Weather();
		 	w.id=o.id;
		 	w.cityid=o.cityid;
		 	w.celcius=o.celcius;
		 	w.forecast=o.forecast;
		 	w.updateTime=o.updateTime;
		 	w.offlineOperator=o.offlineOperator;
		 	w.high=o.high;
		 	w.low=o.low;
		 	w.humidity=o.humidity;
		 	w.visibility=o.visibility;
		 	w.des=o.des;
		 	w.pictureid=o.pictureid;
			return w;
		 }
		 /**
		 * 默认的sql错误处理
		 **/
		 private function sqlErrorHandler(e:SQLErrorEvent):void{
		 	trace(e.text);
		 }
	}
}